package org.jhulk.states;

import java.util.ArrayList;

import org.jhulk.sprites.Squad;
import org.jhulk.utils.GameUtils;
import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.Color;
import org.newdawn.slick.Font;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.XMLPackedSheet;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.GameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.CrossStateTransition;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;
import org.newdawn.slick.tiled.TiledMap;

public class LoadState extends BasicGameState {

	/** The ID given to this state */
	public static final int ID = 0;
	
	private GameCache cache;
	
    /** The font to write the message with */ 
    private Font font; 
	
    /** The game holding this state */ 
    private StateBasedGame game; 
    
    public LoadState(GameCache cache)
    {
    	this.cache = cache;
    }
    
	@Override
	public int getID() {
		return ID;
	}

	public void init(GameContainer container, StateBasedGame game) {	
		try {
			this.game = game; 
			font = new AngelCodeFont("data/fonts/demo2.fnt","data/fonts/demo2_00.tga"); 
			initCache();
		} catch (SlickException e) {
			e.printStackTrace();
		}
	}
	
	private void initCache() throws SlickException
	{
		cache.setMap( new TiledMap("data/maps/testTileset.tmx") );
		XMLPackedSheet sheet = new XMLPackedSheet("data/img/packedTest.png", "data/img/packedTest.png.xml");
		
		String squadString;
		cache.setSquads( new ArrayList<Squad>() );
		squadString = cache.getMap().getMapProperty("squad1", "");
		Squad curSquad =GameUtils.parseSquadString(squadString, sheet);
		cache.getSquads().add(curSquad);
		cache.addToPlacementQueue(curSquad);
	}

	public void render(GameContainer container, StateBasedGame game, Graphics g)
			throws SlickException {
        g.setFont(font); 
        g.setColor(Color.white); 
        g.drawString("State Based Game Test", 125, 100); 
        g.drawString("Press Enter to begin unit placement", 125, 300); 
        g.setColor(Color.red); 
        g.drawString("Now loading the game resouces", 125, 250);
	}

	public void update(GameContainer arg0, StateBasedGame arg1, int arg2)
			throws SlickException {
		// TODO Auto-generated method stub

	}
	
    public void keyReleased(int key, char c) { 
        
        if (key == Input.KEY_ENTER) { 
            GameState target = game.getState(PlacementState.ID); 
             
            final long start = System.currentTimeMillis(); 
            CrossStateTransition t = new CrossStateTransition(target) {                 
                public boolean isComplete() { 
                    return (System.currentTimeMillis() - start) > 2000; 
                } 
 
                public void init(GameState firstState, GameState secondState) {} 
            }; 
             
            game.enterState(PlacementState.ID,  new FadeOutTransition(Color.black), new FadeInTransition(Color.black)); 
        } 
    } 

}
